package rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.dao.ParametriDao;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.dao.VoznjaDao;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Parametri;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Tacka;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Voznja;

@SuppressWarnings("serial")
public class ZavrsavanjeVoznjeKontroler extends HttpServlet {

	@EJB
	private VoznjaDao cVoznjaDao;

	@EJB
	private ParametriDao cParametriLokalno;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String voznjaId = req.getParameter("voznjaId");
		Voznja voznja = cVoznjaDao.pronadjiPoId(Integer.parseInt(voznjaId));

		Parametri parametri = cParametriLokalno.tekuci();
		float a = parametri.getA();
		float b = parametri.getB();
		float c = parametri.getC();
		float predjeniPut = Float
				.parseFloat(req.getParameter("predjeniPut") == null ? "0" : req
						.getParameter("predjeniPut"));
		float vremeVoznje = Float
				.parseFloat(req.getParameter("vremeVoznje") == null ? "0" : req
						.getParameter("vremeVoznje"));
		float cena = a * predjeniPut + b * vremeVoznje + c;

		Tacka odrediste = new Tacka();
		odrediste.setAdresa(req.getParameter("odredisnaAdresa"));
		odrediste.setAdresa(req.getParameter("geografskaSirinaOdredista"));
		odrediste.setAdresa(req.getParameter("geografskaDuzinaOdredista"));

		voznja.setCena(cena);
		voznja.setOdrediste(odrediste);
		cVoznjaDao.stopi(voznja);

		getServletContext().getRequestDispatcher(
				"/jsp/prikazNezapocetihVoznji.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
